import 'package:auto_route/annotations.dart';
import 'package:auto_route/auto_route.dart';
import 'package:base/application.dart';
import 'package:base/generated/l10n.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'common/router/app_router.gr.dart';
import 'common/states/base.dart';

void main() async {
  var widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  await app.init(binding: widgetsBinding);
  runApp(UncontrolledProviderScope(container: app.container, child: const MyApp()));
}

class MyApp extends ConsumerStatefulWidget {
  const MyApp({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends ConsumerState<MyApp> {
  @override
  Widget build(BuildContext context) {
    final themeValue = ref.watch(themeProvider);
    var content = ScreenUtilInit(
      designSize: const Size(375, 812),
      builder: (context, _) {
        final botToastBuilder = BotToastInit();
        return MaterialApp.router(
            title: 'Bugs',
            themeMode: ThemeMode.light,
            localizationsDelegates: const [
              SS.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate
            ],
            supportedLocales: SS.delegate.supportedLocales,
            theme: ThemeData(
                brightness: Brightness.light,
                tabBarTheme: const TabBarTheme(indicatorSize: TabBarIndicatorSize.label),
                textSelectionTheme: TextSelectionThemeData(cursorColor: themeValue.primaryColor),
                inputDecorationTheme: InputDecorationTheme(
                  focusColor: themeValue.primaryColor,
                ),
                colorScheme: ColorScheme.fromSeed(
                    surface: themeValue.backgroundColor,
                    onSurface: themeValue.highBackgroundColor,
                    primary: themeValue.primaryColor,
                    error: themeValue.errorColor,
                    brightness: Brightness.light,
                    seedColor: themeValue.backgroundColor,
                    tertiary: themeValue.lineColor),
                primaryColor: themeValue.primaryColor,
                dividerColor: themeValue.lineColor,
                shadowColor: themeValue.shadowColor,
                textButtonTheme: TextButtonThemeData(
                    style: ButtonStyle(
                        textStyle:
                            WidgetStateProperty.all(TextStyle(color: themeValue.primaryColor)))),
                disabledColor: themeValue.disableColor,
                cardColor: themeValue.backgroundColor,
                useMaterial3: false,
                textTheme: TextTheme(
                  bodyMedium: TextStyle(
                      color: themeValue.primaryTextColor,
                      fontSize: 15.sp,
                      fontWeight: FontWeight.w700),
                  bodyLarge: TextStyle(
                      fontSize: 16.sp,
                      color: themeValue.primaryTextColor,
                      fontWeight: FontWeight.w700),
                  bodySmall: TextStyle(
                      fontSize: 12.sp,
                      color: themeValue.primaryTextColor,
                      fontWeight: FontWeight.w700),
                  labelSmall: TextStyle(
                      color: themeValue.primaryTextGrayColor,
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w500),
                  labelMedium: TextStyle(
                      color: themeValue.primaryTextGrayColor,
                      fontSize: 15.sp,
                      fontWeight: FontWeight.w500),
                  displayLarge: TextStyle(
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w500,
                      color: themeValue.primaryTextColor),
                  displayMedium: TextStyle(
                      fontSize: 15.sp,
                      fontWeight: FontWeight.w500,
                      color: themeValue.primaryTextColor),
                  displaySmall: TextStyle(
                      fontSize: 12.sp,
                      color: themeValue.primaryTextColor,
                      fontWeight: FontWeight.w500),
                ),
                appBarTheme: AppBarTheme(
                    iconTheme: const IconThemeData(color: Colors.black),
                    scrolledUnderElevation: 0,
                    elevation: 0,
                    centerTitle: true,
                    titleTextStyle: TextStyle(
                      fontSize: 18.sp,
                      color: themeValue.primaryTextColor,
                      fontWeight: FontWeight.w600,
                      // fontFamily: "Montserrat"
                    ),
                    backgroundColor: themeValue.backgroundColor,
                    systemOverlayStyle: SystemUiOverlayStyle.dark)),
            routerConfig:
                app.appRouter.config(navigatorObservers: () => [BotToastNavigatorObserver()]),
            builder: (context, child) {
              child = MediaQuery(
                  data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1.0)),
                  child: child!);
              child = botToastBuilder(context, child);
              return child;
            });
      },
    );

    return content;
  }
}

@RoutePage()
class DashboardPage extends StatelessWidget {
  const DashboardPage({super.key});

  @override
  Widget build(BuildContext context) {
    return AutoTabsScaffold(
      animationDuration: Duration.zero,
      routes: const [HomeRoute(), Tab1Route(), Tab2Route()],
      bottomNavigationBuilder: (_, tabsRouter) {
        return BottomNavigationBar(
          items: const [
            BottomNavigationBarItem(
              label: 'Home',
              icon: Icon(Icons.home),
            ),
            BottomNavigationBarItem(
              label: 'Tab1',
              icon: Icon(Icons.meeting_room),
            ),
            BottomNavigationBarItem(
              label: 'Tab2',
              icon: Icon(Icons.person),
            ),
          ],
          currentIndex: tabsRouter.activeIndex,
          onTap: tabsRouter.setActiveIndex,
        );
      },
    );
  }
}
